
import java.util.ArrayList;
import java.sql.*;

public class LigneListe {
  //Attributs

  private int idLigneListe;
  private int idListe;
  private Item elem;
  private User resp;
  private int qte;
  private boolean checked;
  private static final String tableLL = "ap4_sp_ligneListe";

  //Constructeur
  public LigneListe(int idListe, Item elem, User resp, int qte) throws IllegalArgumentException, SQLException {
    if (idListe >= 0) {
      this.idListe = idListe;
    } else {
      throw new IllegalArgumentException("La liste parente est null");
    }

    if (elem != null) {
      this.elem = elem;
    } else {
      throw new IllegalArgumentException("L'item est null");
    }

    if (resp != null) {
      this.resp = resp;
    } else {
      throw new IllegalArgumentException("L'utilisateur est null");
    }

    if (qte > 0) {
      this.qte = qte;
    } else {
      throw new IllegalArgumentException("La quantité doit être supérieure à 0");
    }

    this.createLigneListe();
  }

  private LigneListe(int idLigneListe, int idListe, Item elem, User resp, int qte, boolean checked) {
    this.idLigneListe = idLigneListe;
    this.idListe = idListe;
    this.elem = elem;
    this.resp = resp;
    this.qte = qte;
    this.checked = checked;
  }

  //Accesseurs
  public int getId() {
    return this.idLigneListe;
  }

  public Item getElem() {
    return this.elem;
  }

  public void setElem(Item elem) throws IllegalArgumentException, SQLException {
    if (elem != null) {
      this.elem = elem;
      try {
        bdd db = bdd.getInstance();
        db.update("UPDATE " + tableLL + " SET elem='" + elem.getId() + "' WHERE idLigne=" + this.idLigneListe);
      } catch (SQLException e) {
        throw e;
      }
    } else {
      throw new IllegalArgumentException("L'item est null");
    }
  }

  public User getResp() {
    return this.resp;
  }

  public void setResp(User resp) throws IllegalArgumentException, SQLException {
    if (resp != null) {
      this.resp = resp;
      try {
        bdd db = bdd.getInstance();
        db.update("UPDATE " + tableLL + " SET resp='" + resp.getId() + "' WHERE idLigne=" + this.idLigneListe);
      } catch (SQLException e) {
        throw e;
      }
    } else {
      throw new IllegalArgumentException("L'utilisateur est null");
    }
  }

  public int getQte() {
    return this.qte;
  }

  public void setQte(int qte) throws IllegalArgumentException, SQLException {
    if (qte > 0) {
      this.qte = qte;
      try {
        bdd db = bdd.getInstance();
        db.update("UPDATE " + tableLL + " SET qte='" + qte + "' WHERE idLigne=" + this.idLigneListe);
      } catch (SQLException e) {
        throw e;
      }
    } else {
      throw new IllegalArgumentException("La quantité doit être supérieure à 0");
    }
  }

  public boolean getChecked() {
    return this.checked;
  }

  public void setChecked(boolean checked) throws SQLException {
    this.checked = checked;
    try {
      bdd db = bdd.getInstance();
      db.update("UPDATE " + tableLL + " SET checked='" + checked + "' WHERE idLigne=" + this.idLigneListe);
    } catch (SQLException e) {
      throw e;
    }
  }

  //Methodes
  public void createLigneListe() throws SQLException {
    try {
      bdd db = bdd.getInstance();
      db.update("INSERT INTO " + tableLL + " (idListe, elem, resp, qte, checked) VALUES ('" + this.idListe + "','" + this.elem.getId() + "','" + this.resp.getId() + "','" + this.qte + "',false)");
      this.idLigneListe = this.getLastId();
    } catch (SQLException e) {
      throw e;
    }
  }

  private int getLastId() throws SQLException {
    try {
      bdd db = bdd.getInstance();
      ResultSet res = db.req("SELECT idLigne FROM " + tableLL + " ORDER BY idLigne DESC LIMIT 0,1");
      res.first();
      return res.getInt(1) + 1;
    } catch (SQLException e) {
      throw e;
    }
  }

  public static ArrayList<LigneListe> chargerLignesListe(Liste liste) throws SQLException {
    ArrayList<LigneListe> lignesListe = new ArrayList<LigneListe>();
    try {
      bdd db = bdd.getInstance();
      ArrayList<Categorie> arCat = Categorie.chargerCategories();
      ArrayList<Item> arItem = Item.chargerItems();
      ArrayList<User> arUser = User.chargerUsers();
      ResultSet res = db.req("SELECT * FROM " + tableLL + " WHERE idListe=" + liste.getListID() + " ORDER BY idLigne DESC");
      while (res.next()) {
        lignesListe.add(new LigneListe(res.getInt("idLigne"), res.getInt("idListe"), Item.getItem(res.getInt("elem"), arItem), User.getUser(res.getInt("resp"), arUser), res.getInt("qte"), res.getBoolean("checked")));
      }
      return lignesListe;
    } catch (SQLException e) {
      throw e;
    }
  }

  public void delete() throws SQLException {
    try {
      bdd db = bdd.getInstance();
      db.update("DELETE FROM  " + tableLL + " WHERE idLigne=" + this.idLigneListe);
    } catch (SQLException e) {
      throw e;
    }
  }

  @Override
  public String toString() {
    return "LigneListe{" + "idLigneListe=" + idLigneListe + "elem=" + elem + "resp=" + resp + "qte=" + qte + "checked=" + checked + '}';
  }

  @Override
  public boolean equals(Object obj) {
    LigneListe ll;
    if (obj.getClass() == this.getClass()) {
      ll = (LigneListe) obj;
      if (ll.elem.equals(this.elem) && ll.resp.equals(this.resp) && ll.qte == this.qte && ll.checked == this.checked) {
        return true;
      }
    }
    return false;
  }
}
